<template>
  <div>
    <q-table
      class="bg-white shadow-1"
      :data="data"
      :columns="columns"
      row-key="id"
      selection="multiple"
      :selected.sync="selected"
      :visible-columns="visibleColumns"
      :filter="searchForm"
      :pagination.sync="pagination"
      :rows-per-page-options="[3, 5, 10, 20]"
      no-data-label="暂无数据"
      table-class="h-custom"
      @request="getMain"
    >
    <template v-slot:top-right="props">
       <q-select
        v-model="visibleColumns"
        multiple
        borderless
        options-dense
        :display-value="$t('显示列')"
        emit-value
        map-options
        :options="columns"
        option-value="name"
        class="q-mx-lg"
      >
        <template v-slot:option="scope">
          <q-item
            v-bind="scope.itemProps"
            v-on="scope.itemEvents"
            v-if="!scope.opt.required"
          >
            <q-item-section avatar>
              <q-toggle color="blue" v-model="visibleColumns" :val="scope.opt.name" />
            </q-item-section>
            <q-item-section style="min-width:60px">
              {{ scope.opt.label }}
            </q-item-section>
          </q-item>
        </template>
      </q-select>
        <q-toggle
          v-model="searchToggle"
          left-label
          label="搜索"
          color="blue"
          checked-icon="check"
          unchecked-icon="clear"
          keep-color
        />
        <q-btn
          flat round dense
          :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
          @click="props.toggleFullscreen"
          class="q-ml-md"
        />
    </template>
      <!-- 显示搜索列 -->
      <template v-if="searchToggle" v-slot:top-row="props">
        <q-tr :props="props">
          <q-td>
            <q-btn color="primary" dense flat icon="search" @click="getMain({ pagination: pagination, filter: searchForm })"/>
          </q-td>
          <q-td v-for="col in props.cols" :key="col.name" class="relative-position">
            <!-- <q-btn color="primary" dense flat icon="search" class="absolute-center" v-if="col.name==='action'" @click="getMain({ pagination: pagination, filter: searchForm })"/> -->
            <!-- <el-date-picker  v-else-if="col.name==='createTime'" value-format="yyyy-MM-dd HH:mm:ss" v-model="searchForm.createTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
            <el-date-picker  v-else-if="col.name==='lastUpdateTime'" value-format="yyyy-MM-dd HH:mm:ss" v-model="searchForm.createTime" type="datetime" placeholder="选择日期时间"></el-date-picker> -->
            <q-input dense outlined v-model="searchForm[col.name]"/>
          </q-td>
        </q-tr>
      </template>
      <template v-slot:header="props">
        <q-tr :props="props">
          <q-th>
            <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
          </q-th>
          <q-th v-for="col in props.cols" :key="col.name" :props="props">
            {{ col.label }}
          </q-th>
        </q-tr>
      </template>
      <template v-slot:body="props">
        <q-tr :props="props">
          <q-td width="52">
            <q-checkbox dense v-model="props.selected" />
          </q-td>
          <q-td width="52" v-for="columns in columns" :key="columns.name"  :props="props">
            {{ props.row[columns.name] }}
          </q-td>
        </q-tr>
      </template>
      <template v-slot:pagination="props">
          <q-pagination
            v-model="pagination.page"
            color="blue"
            :max-pages="5"
            :max="pages"
            :boundaryLinks="true"
            :directionLinks="true"
          />
          共 {{ total }} 条
        </template>
    </q-table>
  </div>
</template>

<script>
export default {
  props: {
    url: {
      type: String,
      required: true
    },
    visibleColumns: {
      type: Array,
      default: function () {
        return []
      }
    },
    columns: {
      type: Array,
      default: function () {
        return []
      }
    },
    searchForm: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  computed: {
    pages () {
      return Math.ceil(this.total / this.pagination.rowsPerPage)
    }
  },
  watch: {
    'pagination.page' () {
      this.getMain({
        pagination: this.pagination,
        filter: this.searchForm
      })
    }
  },
  mounted () {
    this.main()
  },
  data () {
    return {
      searchToggle: false,
      selected: [],
      data: [],
      total: 0,
      pagination: {
        page: 1,
        rowsPerPage: 10,
        rowsNumber: 10,
        sortBy: '',
        descending: Boolean
      }
    }
  },
  methods: {
    main () {
      console.log('111111111111111111')
      this.getMain({
        pagination: this.pagination,
        filter: this.searchForm
      })
    },
    async getMain (props) {
      let { page, rowsPerPage, sortBy, descending } = props.pagination, filter = props.filter
      await this.$api({
        method: 'post',
        url: this.url,
        params: {
          pageNo: page,
          pageSize: rowsPerPage,
          sort: sortBy,
          order: descending === true ? 'desc' : (descending === false ? 'asc' : ''),
          ...filter
        },
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          console.log(data, 11111111111111)
          this.total = data.total
          this.data = data.list
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
      this.pagination.page = page
      this.pagination.rowsPerPage = rowsPerPage
      this.pagination.sortBy = sortBy
      this.pagination.descending = descending
      console.log(this.pagination)
    }
  }
}
</script>

<style lang="stylus" scoped>

</style>
